package com.af.monitorModule.generator.service.impl;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.Company;
import com.af.monitorModule.generator.entity.Server;
import com.af.monitorModule.generator.mapper.CompanyMapper;
import com.af.monitorModule.generator.mapper.ServerMapper;
import com.af.monitorModule.generator.service.CompanyService;
import com.af.monitorModule.response.exception.CustomerException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyz_4dou
 * @since 2021-02-23
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private ServerMapper serverMapper;

    @Override
    public JSONObject findCom(String page, String limit, JSONObject search) {
        Page<Company> p = new Page<Company>(Integer.parseInt(page), Integer.parseInt(limit));
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("create_date");
        if (search != null)
            search.forEach((k,v)->{
                if (!"".equals(v.toString().trim())){
                    switchParam(k,v.toString(),wrapper);
                }
            });
        companyMapper.selectPage(p,wrapper);
        JSONObject res = new JSONObject();
        res.put("data",p.getRecords());
        res.put("code",0);
        res.put("count",p.getTotal());
        res.put("msg","用户列表");
        return res;
    }

    @Override
    public JSONObject changeState(String company) {
        Integer id = Integer.valueOf(company);
        Company change = companyMapper.selectById(id);
        if (change == null){
            return ResultEnum.COMPANY_ISNOT_EXIST.getResult();
        }
        change.setState(change.getState() == 1 ? 0:1);
        companyMapper.updateById(change);
        return ResultEnum.OK.getResult();
    }

    @Override
    public JSONObject findAllCom() {
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper.eq("state",1);
        List<Company> companies = companyMapper.selectList(wrapper);

        JSONObject res= new JSONObject();
        res.put("code",200);
        res.put("data",companies);
        return res;
    }

    @Override
    public JSONObject updateCompany(Company company, String id) {
        if (company == null || id == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        company.setId(Integer.valueOf(id));
        int i = companyMapper.updateById(company);
        return i == 1 ? ResultEnum.OK.getResult() : ResultEnum.VERSION_NO_CORRESPONDING.getResult();
    }

    @Override
    public JSONObject batchStop(JSONObject ids) {
        if (ids == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        JSONArray ids1 = ids.getJSONArray("ids");
        StringBuffer stringBuffer = new StringBuffer();
        ids1.forEach((v)->{
            stringBuffer.append(","+v);
        });
        companyMapper.stopBatch("(" + stringBuffer.toString().substring(1) + ")");
        return ResultEnum.OK.getResult();
    }

    @Override
    public JSONObject findAllServer(String id) {
        if (id == null){
            return  ResultEnum.PARAM_INCORRECRT.getResult();
        }
        QueryWrapper<Server> wrapper = new QueryWrapper();
        wrapper.eq("company_id",Integer.valueOf(id));
        wrapper.eq("state",1);
        List<Server> servers = serverMapper.selectList(wrapper);
        JSONObject res = new JSONObject();
        res.put("data",servers);
        res.put("code",0);
        res.put("count",servers.size());
        res.put("msg","服务器列表");
        return res;
    }

    @Override
    public JSONObject findAllComQR() {
        JSONObject res = new JSONObject();
        res.put("data",companyMapper.getAllComRQCode());
        res.put("code",0);
        res.put("msg","服务器二维码列表");
        return res;
    }


    /**
     * 用户管理页面查询参数添加
     * @param key
     * @param value
     * @param wrapper
     */
    public void switchParam(String key,String value ,QueryWrapper wrapper){
        switch (key){
            case "company_name":{
                wrapper.like("company_name",value);
                break;
            }
            default:{
                wrapper.eq(key,value);
                break;
            }
        }
    }
}
